<?php
namespace app\service;
use think\Request,
	app\model\Unit,
	app\model\Product,
	app\model\OrderInfo,
	app\model\OrderGood,
	app\validate\UnitValidate;

class OrderService extends BaseService
{

    public function page(){

    	$data 	= Request::instance()->get();
    	$where 	= [];

    	//封装where查询条件
    	(empty($data['status']) 	|| $data['status'] == '')	|| $where['status'] 	= 	$data['status'];
    	empty($data['product_no'])	|| $where['product_no']		= 	['like','%'.$data['product_no'] ];
    	empty($data['sn'])			|| $where['order_sn'] 		= 	$data['sn'];

    	if( !$this->user_info->isRootUser() ){
    		$where['company'] 	= 	$this->user_info->company;
    	}

        return OrderInfo::where($where)->paginate(10);     
    }


    public function getNew(){
    	$where 	= [];
    	if( !$this->user_info->isRootUser() ){
    		$where['company'] 	= 	$this->user_info->company;
    	}

        return OrderInfo::where($where)->order('order_id desc')->paginate(10);     
    }


    // 保存数据
    public function save(){
    	Request::instance()->isPost() || die('request not  post!');
    	
		$param = Request::instance()->param();	//获取参数
		$error = $this->_validate($param); 		// 是否通过验证

		if( is_null( $error ) ){

			$product = Product::get([
				'sn'	=>	$param['product_no']
			]);

			//更新产品数量
			$product->num += $param['goods_number'];
			$product->save();

			// dump($param['product_no']);
			// dump($product->price);
			// die;
			if( is_null($product) ){
				$product->id = 1;
				return [
					'error'	=>	100,
					'msg'	=>	'商品编号不存在'
				];
			}

			$res = OrderGood::get([
				'order_sn'	=>	$param['order_sn'],
				'company'	=>	$this->user_info->company
			]);


			//导入批次
			$batch_no = date('Ymdhis',time());

			if( is_null($res) ){
				// $orderInfo 			= new OrderInfo();
				// $orderInfo->name 	= $param['name'];
				// $orderInfo->desc 	= $param['desc'];
				// $orderInfo->status 	= $param['status'];
				// $orderInfo->company = $this->user_info->company;
				// $orderInfo->add_time = time();

				// // 检测错误
				// if( $orderInfo->save() ){
				// 	return ['error'	=>	0,'msg'	=>	'保存成功'];
				// }else{
				// 	return ['error'	=>	100,'msg'	=>	'保存失败'];	
				// }
				
				$res = new OrderGood();
				$res->order_sn 			= $param['order_sn'];
				$res->referer			= $param['referer'];
			    $res->waybillno 		= $param['waybillno'];
			    $res->batch_no 			= $batch_no;
			    $res->identify_no 		= $param['identify_no'];
			    $res->author 			= '';
			    $res->receiver_name 	= $param['receiver_name'];
			    $res->receiver_phone 	= $param['receiver_phone'];
			    $res->receiver_province = $param['receiver_province'];
			    $res->receiver_city 	= $param['receiver_city'];
			    $res->receiver_district = $param['receiver_district'];
			    $res->receiver_address 	= $param['receiver_address'];
			    $res->sender_name 		= $param['sender_name'];
			    $res->sender_phone 		= $param['sender_phone'];
			    $res->sender_address 	= $param['sender_address'];
			    $res->company 			= $this->user_info->company;
			    $res->add_time 			= time();
				$res->save();
			}

			OrderInfo::create([
				'o_id'				=>  $res->rec_id,
			    'batch_no'  		=>  $batch_no,
			    'order_sn'			=>  $param['order_sn'],
			    'referer'			=>	$param['referer'],
			    'waybillno'			=>	$param['waybillno'],
			    'product_no'		=>	$param['product_no'],
			    'receiver_name'		=>	$param['receiver_name'],
			    'identify_no'		=>	$param['identify_no'],
			    'receiver_province'	=>	$param['receiver_province'],
			    'receiver_city'		=>	$param['receiver_city'],
			    'receiver_district'	=>	$param['receiver_district'],
			    'receiver_address'	=>	$param['receiver_address'],
			    'receiver_phone'	=>	$param['receiver_phone'],
			    'sender_name'		=>	$param['sender_name'],
			    'sender_address'	=>	$param['sender_address'],
			    'sender_phone'		=>	$param['sender_phone'],
			    'r_weight'			=>	$param['r_weight'],
			    'goods_number'		=>	$param['goods_number'],
			    'goods_amount'		=>	$product->price*$param['goods_number'],
			    'currency'			=>	$product->price,
			    'remark'			=>	$param['remark'],
			    'company' 			=> 	$this->user_info->company,
			    'add_time'			=>	time()
			]);
			return ['error'	=>	0,'msg'	=>	'添加成功'];
		}else{
			return ['error'	=>	100,'msg'	=>	$error];
		}

    }


    public function edit($id){
    	return OrderInfo::get([
    		'o_id'	=> $id
    	]);
    }


    public function update(){
    	Request::instance()->isPost() || die('request not  post!');
    	
		$param = Request::instance()->param();	//获取参数
		$error = $this->_validate($param); 		// 是否通过验证

		if( is_null( $error ) ){

			$product = Product::get([
				'sn'	=>	$param['product_no']
			]);


			if( is_null($product) ){
				$product->id = 1;
				return [
					'error'	=>	100,
					'msg'	=>	'商品编号不存在'
				];
			}

			$res = OrderInfo::get([
				'order_sn'	=>	$param['order_sn'],
			]);

		// dump($res->order_sn);
		// die;
			$res->order_sn			=	$param['order_sn'];
			$res->referer			=	$param['referer'];
			$res->waybillno			=	$param['waybillno'];
			$res->product_no		=	$param['product_no'];
			$res->receiver_name		=	$param['receiver_name'];
			$res->identify_no		=	$param['identify_no'];
			$res->receiver_province	=	$param['receiver_province'];
			$res->receiver_city		=	$param['receiver_city'];
			$res->receiver_district	=	$param['receiver_district'];
			$res->receiver_address	=	$param['receiver_address'];
			$res->receiver_phone	=	$param['receiver_phone'];
			$res->sender_name		=	$param['sender_name'];
			$res->sender_address	=	$param['sender_address'];
			$res->sender_phone		=	$param['sender_phone'];
			$res->r_weight			=	$param['r_weight'];
			$res->currency			=	$product->price;
			$res->remark			=	$param['remark'];

			if( $res->goods_number > $param['goods_number'] ){
				$res->goods_number		-=	($res->goods_number-$param['goods_number']);
				//更新产品数量
				$product->num -= ($res->goods_number-$param['goods_number']);
				
			}elseif($res->goods_number < $param['goods_number']) {
				$res->goods_number		+=	($param['goods_number']-$res->goods_number);
				//更新产品数量
				$product->num +=	($param['goods_number']-$res->goods_number);
			}else{
				$res->goods_number = $param['goods_number'];
				//更新产品数量
				$product->num = $param['goods_number'];
			}

			$product->save();

			$res->goods_amount		=	$product->price*$param['goods_number'];

			return ['error'	=>	0,'msg'	=>	'修改成功'];
		}else{
			return ['error'	=>	100,'msg'	=>	$error];
		}


    }


    public function delete($id){

    	// $order_good = OrderGood::get([
    	// 	'rec_id'	=>	$id
    	// ]);
    	

    	// OrderGood::destroy($id);
    	// $info = OrderInfo::get([
    	// 	'o_id'	=>	$id
    	// ]);    	

    	$info = OrderInfo::get([
    		'order_id'	=>	$id
    	]);

    	// dump($info);
   //  	foreach ($info as $v) {
   //  		dump($v->product_no);
			// $product = Product::get([
			// 	'sn'	=>	$v->product_no
			// ]);
			// $product->num
   //  	}

    	$res = $info->delete();
    	if( $res ){
    		return ['error'	=>	0,'msg'	=>	'删除成功'];
    	}else{
    		return ['error'	=>	100,'msg'	=>	'删除失败'];	
    	}

		// 支持批量删除多个数据
		// User::destroy('1,2,3');
		// // 或者
		// User::destroy([1,2,3]);

    }


    // 验证器
    private function _validate($data){
		// 验证
		$validate = validate('OrderInfoValidate');
		if(!$validate->check($data)){
			return $validate->getError();
		}
    }

    //文件上传
    public function upload($file){
    	Request::instance()->isPost() || die('request not  post!');    

	    // 移动到框架应用根目录/public/uploads/ 目录下
	    if($file){
	        // $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
	        // 移动到框架应用根目录/public/uploads/ 目录下
    		$info = $file->validate(['ext'=>'xls,xlsx'])->move(ROOT_PATH . 'public' . DS . 'uploads');
	        if($info){
	            // 成功上传后 获取上传信息
	            // 输出文件后缀 xls
	            // echo $info->getExtension();

	            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
	            // echo $info->getSaveName();
	            
	            $excel = new ExcelService();
		        $excel->read(ROOT_PATH.'public'.DS.'uploads/'.$info->getSaveName() ) ;
		       	return true;

	            // 输出 42a79759f284b767dfcb2a0197904287.jpg
	            // echo $info->getFilename(); 
	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	        }
	    }

    }

 //    public function uploads($file){    
 //        $excel = new ExcelService();
 //        $excel->read() ;
 //        return true;
	// }	       	

}
